<template><div><p>本文档最新版为 <a href="https://learnku.com/docs/laravel/10.x" target="_blank" rel="noopener noreferrer">10.x</a>，旧版本可能放弃维护，推荐阅读最新版！</p>
<h2 id="prompts" tabindex="-1"><a class="header-anchor" href="#prompts"><span>Prompts</span></a></h2>
<ul>
<li><a href="#introduction">介绍</a></li>
<li><a href="#installation">安装</a></li>
<li><a href="#available-prompts">可用 Prompts</a>
<ul>
<li><a href="#text">文本</a></li>
<li><a href="#textarea">文本框</a></li>
<li><a href="#password">密码</a></li>
<li><a href="#confirm">确认</a></li>
<li><a href="#select">单选</a></li>
<li><a href="#multiselect">多选</a></li>
<li><a href="#suggest">建议</a></li>
<li><a href="#search">搜索</a></li>
<li><a href="#multisearch">多选搜索</a></li>
<li><a href="#pause">暂停</a></li>
</ul>
</li>
<li><a href="#forms">表单</a></li>
<li><a href="#informational-messages">提示信息</a></li>
<li><a href="#tables">表格</a></li>
<li><a href="#spin">旋转</a></li>
<li><a href="#progress">进度条</a></li>
<li><a href="#terminal-considerations">终端考虑因素</a></li>
<li><a href="#fallbacks">不支持的环境与回退</a></li>
</ul>
<h2 id="介绍" tabindex="-1"><a class="header-anchor" href="#介绍"><span>介绍</span></a></h2>
<p><a href="https://github.com/laravel/prompts" target="_blank" rel="noopener noreferrer">Laravel Prompts</a> 是一个 PHP 包，用于向命令行应用程序添加漂亮且用户友好的表单，并具有类似浏览器的特性，包括占位符文本和验证。</p>
<p><img src="https://laravel.com/img/docs/prompts-example.png" alt="prompts-example.png"></p>
<p>Laravel Prompts 非常适合在你的 <a href="https://learnku.com/docs/laravel/11.x/artisan#writing-commands" target="_blank" rel="noopener noreferrer">Artisan 控制台命令</a> 中接收用户输入，但是它也可以用在任何命令行 PHP 项目中。</p>
<blockquote>
<p><strong>注意</strong><br>
Laravel Prompts 通过 WSL 支持 macOS、 Linux 和 Windows。有关更多信息，请参阅我们的文档 <a href="#fallbacks">不支持的环境 &amp; 回退</a>。</p>
</blockquote>
<h2 id="安装" tabindex="-1"><a class="header-anchor" href="#安装"><span>安装</span></a></h2>
<p>Laravel Prompts 已经包含在最新版本的 Laravel 中。</p>
<p>你也可以通过使用 Composer 包管理器，将 Laravel Prompts 安装在你其他 PHP 项目中:</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token function">composer</span> require laravel/prompts</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="可用提示" tabindex="-1"><a class="header-anchor" href="#可用提示"><span>可用提示</span></a></h2>
<h3 id="text" tabindex="-1"><a class="header-anchor" href="#text"><span>Text</span></a></h3>
<p><code v-pre>text</code> 函数将提示用户指定的问题，接收他们的输入，然后返回:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token keyword">function</span> <span class="token package">Laravel<span class="token punctuation">\</span>Prompts<span class="token punctuation">\</span>text</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$name</span> <span class="token operator">=</span> <span class="token function">text</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'What is your name?'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你还可以引入占位文本、默认值和信息提示:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$name</span> <span class="token operator">=</span> <span class="token function">text</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token argument-name">label</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'What is your name?'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">placeholder</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'E.g. Taylor Otwell'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token keyword">default</span><span class="token punctuation">:</span> <span class="token variable">$user</span><span class="token operator">?-></span><span class="token property">name</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">hint</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'This will be displayed on your profile.'</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="必传的值" tabindex="-1"><a class="header-anchor" href="#必传的值"><span>必传的值</span></a></h4>
<p>如果你要求一个值必传，可以传递 <code v-pre>required</code> 参数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$name</span> <span class="token operator">=</span> <span class="token function">text</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token argument-name">label</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'What is your name?'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">required</span><span class="token punctuation">:</span> <span class="token constant boolean">true</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果你想自定义验证信息，你也可以传递一个字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$name</span> <span class="token operator">=</span> <span class="token function">text</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token argument-name">label</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'What is your name?'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">required</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'Your name is required.'</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="额外验证" tabindex="-1"><a class="header-anchor" href="#额外验证"><span>额外验证</span></a></h4>
<p>最后，如果你想执行额外的验证逻辑，可以给 <code v-pre>validate</code> 参数传递一个闭包：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$name</span> <span class="token operator">=</span> <span class="token function">text</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token argument-name">label</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'What is your name?'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">validate</span><span class="token punctuation">:</span> <span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token keyword type-hint">string</span> <span class="token variable">$value</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token keyword">match</span> <span class="token punctuation">(</span><span class="token constant boolean">true</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token function">strlen</span><span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">)</span> <span class="token operator">&lt;</span> <span class="token number">3</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'The name must be at least 3 characters.'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token function">strlen</span><span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">)</span> <span class="token operator">></span> <span class="token number">255</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'The name must not exceed 255 characters.'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token keyword">default</span> <span class="token operator">=></span> <span class="token constant">null</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>闭包接收输入的值，可能会返回一个错误信息，如果验证通过会返回 <code v-pre>null</code>。</p>
<p>或者，你也可以使用 Laravel <a href="https://learnku.com/docs/laravel/11.x/validation" target="_blank" rel="noopener noreferrer">验证器</a> 这个强大功能。要想使用它，你可以给 <code v-pre>validate</code> 传递一个数组，数组包含属性名称和所需的验证规则：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$name</span> <span class="token operator">=</span> <span class="token function">text</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token argument-name">label</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'What is your name?'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">validate</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required|max:255|unique:users,name'</span><span class="token punctuation">]</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="文本框" tabindex="-1"><a class="header-anchor" href="#文本框"><span>文本框</span></a></h3>
<p><code v-pre>textarea</code> 函数给用户提示指定的问题，通过多行文本框接收输入，然后返回：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token keyword">function</span> <span class="token package">Laravel<span class="token punctuation">\</span>Prompts<span class="token punctuation">\</span>textarea</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$story</span> <span class="token operator">=</span> <span class="token function">textarea</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Tell me a story.'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你也可以引入占位文本、默认值和信息提示：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$story</span> <span class="token operator">=</span> <span class="token function">textarea</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token argument-name">label</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'Tell me a story.'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">placeholder</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'This is a story about...'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">hint</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'This will be displayed on your profile.'</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="必传的值-1" tabindex="-1"><a class="header-anchor" href="#必传的值-1"><span>必传的值</span></a></h4>
<p>如果你要求一个值必传，可以传递 <code v-pre>required</code> 参数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$story</span> <span class="token operator">=</span> <span class="token function">textarea</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token argument-name">label</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'Tell me a story.'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">required</span><span class="token punctuation">:</span> <span class="token constant boolean">true</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果你想自定义验证信息，你也可以传递一个字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$story</span> <span class="token operator">=</span> <span class="token function">textarea</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token argument-name">label</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'Tell me a story.'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">required</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'A story is required.'</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="额外验证-1" tabindex="-1"><a class="header-anchor" href="#额外验证-1"><span>额外验证</span></a></h4>
<p>最后，如果你想执行额外的验证逻辑，可以给 <code v-pre>validate</code> 参数传递一个闭包：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$story</span> <span class="token operator">=</span> <span class="token function">textarea</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token argument-name">label</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'Tell me a story.'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">validate</span><span class="token punctuation">:</span> <span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token keyword type-hint">string</span> <span class="token variable">$value</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token keyword">match</span> <span class="token punctuation">(</span><span class="token constant boolean">true</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token function">strlen</span><span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">)</span> <span class="token operator">&lt;</span> <span class="token number">250</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'The story must be at least 250 characters.'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token function">strlen</span><span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">)</span> <span class="token operator">></span> <span class="token number">10000</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'The story must not exceed 10,000 characters.'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token keyword">default</span> <span class="token operator">=></span> <span class="token constant">null</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>闭包接收输入的值，可能会返回一个错误信息，如果验证通过会返回 <code v-pre>null</code>。</p>
<p>或者，你也可以使用 Laravel <a href="https://learnku.com/docs/laravel/11.x/validation" target="_blank" rel="noopener noreferrer">验证器</a> 这个强大功能。要想使用它，你可以给 <code v-pre>validate</code> 传递一个数组，数组包含属性名称和所需的验证规则：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$story</span> <span class="token operator">=</span> <span class="token function">textarea</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token argument-name">label</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'Tell me a story.'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">validate</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'story'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required|max:10000'</span><span class="token punctuation">]</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="密码" tabindex="-1"><a class="header-anchor" href="#密码"><span>密码</span></a></h3>
<p><code v-pre>password</code> 函数和 <code v-pre>text</code> 函数类似，但是当用户在控制台输入时，内容将被遮蔽。这在询问诸如密码这种敏感信息时十分有用：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token keyword">function</span> <span class="token package">Laravel<span class="token punctuation">\</span>Prompts<span class="token punctuation">\</span>password</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$password</span> <span class="token operator">=</span> <span class="token function">password</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'What is your password?'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你也可以引入占位文本、默认值和信息提示：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$password</span> <span class="token operator">=</span> <span class="token function">password</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token argument-name">label</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'What is your password?'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">placeholder</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'password'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">hint</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'Minimum 8 characters.'</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="必传的值-2" tabindex="-1"><a class="header-anchor" href="#必传的值-2"><span>必传的值</span></a></h4>
<p>如果你要求一个值必传，可以传递 <code v-pre>required</code> 参数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$password</span> <span class="token operator">=</span> <span class="token function">password</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token argument-name">label</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'What is your password?'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">required</span><span class="token punctuation">:</span> <span class="token constant boolean">true</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果你想自定义验证信息，你也可以传递一个字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$password</span> <span class="token operator">=</span> <span class="token function">password</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token argument-name">label</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'What is your password?'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">required</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'The password is required.'</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="额外验证-2" tabindex="-1"><a class="header-anchor" href="#额外验证-2"><span>额外验证</span></a></h4>
<p>最后，如果你想执行额外的验证逻辑，可以给 <code v-pre>validate</code> 参数传递一个闭包：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$password</span> <span class="token operator">=</span> <span class="token function">password</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token argument-name">label</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'What is your password?'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">validate</span><span class="token punctuation">:</span> <span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token keyword type-hint">string</span> <span class="token variable">$value</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token keyword">match</span> <span class="token punctuation">(</span><span class="token constant boolean">true</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token function">strlen</span><span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">)</span> <span class="token operator">&lt;</span> <span class="token number">8</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'The password must be at least 8 characters.'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token keyword">default</span> <span class="token operator">=></span> <span class="token constant">null</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>闭包接收输入的值，可能会返回一个错误信息，如果验证通过会返回 <code v-pre>null</code>。</p>
<p>或者，你也可以使用 Laravel <a href="https://learnku.com/docs/laravel/11.x/validation" target="_blank" rel="noopener noreferrer">验证器</a> 这个强大功能。要想使用它，你可以给 <code v-pre>validate</code> 传递一个数组，数组包含属性名称和所需的验证规则：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$password</span> <span class="token operator">=</span> <span class="token function">password</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token argument-name">label</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'What is your password?'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">validate</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'password'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'min:8'</span><span class="token punctuation">]</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="确认" tabindex="-1"><a class="header-anchor" href="#确认"><span>确认</span></a></h3>
<p>如果你需要询问用户一个「是」或「否」的确认，可以使用 <code v-pre>confirm</code> 函数。用户可以使用箭头键或者字母 <code v-pre>y</code> 或者 <code v-pre>n</code> 键，来选择他们的响应。该函数会返回 <code v-pre>true</code> 或者 <code v-pre>false</code>。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token keyword">function</span> <span class="token package">Laravel<span class="token punctuation">\</span>Prompts<span class="token punctuation">\</span>confirm</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$confirmed</span> <span class="token operator">=</span> <span class="token function">confirm</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Do you accept the terms?'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你也可以引入默认值，「是」和「否」标签的自定义措辞，以及信息提示：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$confirmed</span> <span class="token operator">=</span> <span class="token function">confirm</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token argument-name">label</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'Do you accept the terms?'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token keyword">default</span><span class="token punctuation">:</span> <span class="token constant boolean">false</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">yes</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'I accept'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">no</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'I decline'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">hint</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'The terms must be accepted to continue.'</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="强制选择「是」" tabindex="-1"><a class="header-anchor" href="#强制选择「是」"><span>强制选择「是」</span></a></h4>
<p>如有必要，你可以通过传递 <code v-pre>required</code> 参数强制用户选择「是」：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$confirmed</span> <span class="token operator">=</span> <span class="token function">confirm</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token argument-name">label</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'Do you accept the terms?'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">required</span><span class="token punctuation">:</span> <span class="token constant boolean">true</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果你想自定义验证信息，你也可以传递一个字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$confirmed</span> <span class="token operator">=</span> <span class="token function">confirm</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token argument-name">label</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'Do you accept the terms?'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">required</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'You must accept the terms to continue.'</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="单选" tabindex="-1"><a class="header-anchor" href="#单选"><span>单选</span></a></h3>
<p>如果你需要用户从一组预定义选项中进行选择，可以使用 <code v-pre>select</code> 函数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token keyword">function</span> <span class="token package">Laravel<span class="token punctuation">\</span>Prompts<span class="token punctuation">\</span>select</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$role</span> <span class="token operator">=</span> <span class="token function">select</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token string single-quoted-string">'What role should the user have?'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">[</span><span class="token string single-quoted-string">'Member'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Contributor'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Owner'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你也可以指定默认选项和信息提示：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$role</span> <span class="token operator">=</span> <span class="token function">select</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token argument-name">label</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'What role should the user have?'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">options</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'Member'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Contributor'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Owner'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token keyword">default</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'Owner'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">hint</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'The role may be changed at any time.'</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你也可以给 <code v-pre>options</code> 传递一个关联数组，让选中的键返回，而不是返回它的值:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$role</span> <span class="token operator">=</span> <span class="token function">select</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token argument-name">label</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'What role should the user have?'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">options</span><span class="token punctuation">:</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'member'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Member'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'contributor'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Contributor'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'owner'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Owner'</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token keyword">default</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'owner'</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>当列表开始滚动前，最多展示五个选项。可以通过传递 <code v-pre>scroll</code> 参数自定义这个值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$role</span> <span class="token operator">=</span> <span class="token function">select</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token argument-name">label</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'Which category would you like to assign?'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">options</span><span class="token punctuation">:</span> <span class="token class-name static-context">Category</span><span class="token operator">::</span><span class="token function">pluck</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'id'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">scroll</span><span class="token punctuation">:</span> <span class="token number">10</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="验证" tabindex="-1"><a class="header-anchor" href="#验证"><span>验证</span></a></h4>
<p>不同于其他提示函数，<code v-pre>select</code> 函数不接受 <code v-pre>required</code> 参数，因为什么都不选是不可能的。然而，如果你需要展示一个选项但又阻止其被选中，可以给 <code v-pre>validate</code> 参数传递一个闭包：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$role</span> <span class="token operator">=</span> <span class="token function">select</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token argument-name">label</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'What role should the user have?'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">options</span><span class="token punctuation">:</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'member'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Member'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'contributor'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Contributor'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'owner'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Owner'</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">validate</span><span class="token punctuation">:</span> <span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token keyword type-hint">string</span> <span class="token variable">$value</span><span class="token punctuation">)</span> <span class="token operator">=></span></span>
<span class="line">        <span class="token variable">$value</span> <span class="token operator">===</span> <span class="token string single-quoted-string">'owner'</span> <span class="token operator">&amp;&amp;</span> <span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'role'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'owner'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">exists</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">?</span> <span class="token string single-quoted-string">'An owner already exists.'</span></span>
<span class="line">            <span class="token punctuation">:</span> <span class="token constant">null</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果 <code v-pre>options</code> 参数是一个关联数组，闭包将接收选中的键，否则将接收选中的值。闭包可能会返回一个错误信息，如果验证通过则返回 <code v-pre>null</code>。</p>
<h3 id="多选" tabindex="-1"><a class="header-anchor" href="#多选"><span>多选</span></a></h3>
<p>如果你需要用户能够选择多个选项，可以使用 <code v-pre>multiselect</code> 函数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token keyword">function</span> <span class="token package">Laravel<span class="token punctuation">\</span>Prompts<span class="token punctuation">\</span>multiselect</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$permissions</span> <span class="token operator">=</span> <span class="token function">multiselect</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token string single-quoted-string">'What permissions should be assigned?'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">[</span><span class="token string single-quoted-string">'Read'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Create'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Update'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Delete'</span><span class="token punctuation">]</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你也可以指定默认选项和信息提示：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token keyword">function</span> <span class="token package">Laravel<span class="token punctuation">\</span>Prompts<span class="token punctuation">\</span>multiselect</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$permissions</span> <span class="token operator">=</span> <span class="token function">multiselect</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token argument-name">label</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'What permissions should be assigned?'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">options</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'Read'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Create'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Update'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Delete'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token keyword">default</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'Read'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Create'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">hint</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'Permissions may be updated at any time.'</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你也可以给 <code v-pre>options</code> 传递一个关联数组，返回选中选项的键，而不是它们的值:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$permissions</span> <span class="token operator">=</span> <span class="token function">multiselect</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token argument-name">label</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'What permissions should be assigned?'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">options</span><span class="token punctuation">:</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'read'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Read'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'create'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Create'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'update'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Update'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'delete'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Delete'</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token keyword">default</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'read'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'create'</span><span class="token punctuation">]</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>当列表开始滚动前，最多展示五个选项。可以通过传递 <code v-pre>scroll</code> 参数自定义这个值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$categories</span> <span class="token operator">=</span> <span class="token function">multiselect</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token argument-name">label</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'What categories should be assigned?'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">options</span><span class="token punctuation">:</span> <span class="token class-name static-context">Category</span><span class="token operator">::</span><span class="token function">pluck</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'id'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">scroll</span><span class="token punctuation">:</span> <span class="token number">10</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你也可用通过 <code v-pre>canSelectAll</code> 参数，让用户轻松选择所有选项：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$categories</span> <span class="token operator">=</span> <span class="token function">multiselect</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token argument-name">label</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'What categories should be assigned?'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">options</span><span class="token punctuation">:</span> <span class="token class-name static-context">Category</span><span class="token operator">::</span><span class="token function">pluck</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'id'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">canSelectAll</span><span class="token punctuation">:</span> <span class="token constant boolean">true</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="强制选择至少一个值" tabindex="-1"><a class="header-anchor" href="#强制选择至少一个值"><span>强制选择至少一个值</span></a></h4>
<p>默认情况下，用户可以选择零个或多个选项。你可以通过传递 <code v-pre>required</code> 参数，强制用户选择一个或多个选项：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$categories</span> <span class="token operator">=</span> <span class="token function">multiselect</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token argument-name">label</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'What categories should be assigned?'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">options</span><span class="token punctuation">:</span> <span class="token class-name static-context">Category</span><span class="token operator">::</span><span class="token function">pluck</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'id'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">required</span><span class="token punctuation">:</span> <span class="token constant boolean">true</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果你想自定义验证信息，可以给 <code v-pre>required</code> 参数传递一个字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$categories</span> <span class="token operator">=</span> <span class="token function">multiselect</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token argument-name">label</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'What categories should be assigned?'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">options</span><span class="token punctuation">:</span> <span class="token class-name static-context">Category</span><span class="token operator">::</span><span class="token function">pluck</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'id'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">required</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'You must select at least one category'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="验证-1" tabindex="-1"><a class="header-anchor" href="#验证-1"><span>验证</span></a></h4>
<p>如果你想展示一个选项又想阻止其被选中的话，可以给 <code v-pre>validate</code> 传递一个闭包：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$permissions</span> <span class="token operator">=</span> <span class="token function">multiselect</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token argument-name">label</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'What permissions should the user have?'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">options</span><span class="token punctuation">:</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'read'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Read'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'create'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Create'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'update'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Update'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'delete'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Delete'</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">validate</span><span class="token punctuation">:</span> <span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token keyword type-hint">array</span> <span class="token variable">$values</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token operator">!</span> <span class="token function">in_array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'read'</span><span class="token punctuation">,</span> <span class="token variable">$values</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token operator">?</span> <span class="token string single-quoted-string">'All users require the read permission.'</span></span>
<span class="line">        <span class="token punctuation">:</span> <span class="token constant">null</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果 <code v-pre>options</code> 参数是一个关联数组，闭包将接收选中的键，否则将接收选中的值。闭包可能会返回一个错误信息，如果验证通过则返回 <code v-pre>null</code>。</p>
<h3 id="建议" tabindex="-1"><a class="header-anchor" href="#建议"><span>建议</span></a></h3>
<p><code v-pre>suggest</code> 函数可以用于为可能的选项提供自动补全功能。不过用户仍然可以忽视自动补全的提示，提供任意一个答案：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token keyword">function</span> <span class="token package">Laravel<span class="token punctuation">\</span>Prompts<span class="token punctuation">\</span>suggest</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$name</span> <span class="token operator">=</span> <span class="token function">suggest</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'What is your name?'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'Taylor'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Dayle'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>另外，你也可以给 <code v-pre>suggest</code> 函数传递一个闭包作为第二个参数。当用户每次输入一个字符时，闭包将会被调用。闭包应该接收一个字符串参数，其中包含用户到目前为止的输入，并返回一个自动完成选项的数组：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$name</span> <span class="token operator">=</span> <span class="token function">suggest</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token string single-quoted-string">'What is your name?'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token function">collect</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'Taylor'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Dayle'</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token operator">-></span><span class="token function">filter</span><span class="token punctuation">(</span><span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token variable">$name</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">contains</span><span class="token punctuation">(</span><span class="token variable">$name</span><span class="token punctuation">,</span> <span class="token variable">$value</span><span class="token punctuation">,</span> <span class="token argument-name">ignoreCase</span><span class="token punctuation">:</span> <span class="token constant boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">)</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你也可以引入占位文本、默认值和信息提示：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$name</span> <span class="token operator">=</span> <span class="token function">suggest</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token argument-name">label</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'What is your name?'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">options</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'Taylor'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Dayle'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">placeholder</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'E.g. Taylor'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token keyword">default</span><span class="token punctuation">:</span> <span class="token variable">$user</span><span class="token operator">?-></span><span class="token property">name</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">hint</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'This will be displayed on your profile.'</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="必传的值-3" tabindex="-1"><a class="header-anchor" href="#必传的值-3"><span>必传的值</span></a></h4>
<p>如果你要求一个值必传，可以传递 <code v-pre>required</code> 参数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$name</span> <span class="token operator">=</span> <span class="token function">suggest</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token argument-name">label</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'What is your name?'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">options</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'Taylor'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Dayle'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">required</span><span class="token punctuation">:</span> <span class="token constant boolean">true</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果你想自定义验证信息，你也可以传递一个字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$name</span> <span class="token operator">=</span> <span class="token function">suggest</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token argument-name">label</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'What is your name?'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">options</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'Taylor'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Dayle'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">required</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'Your name is required.'</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="额外验证-3" tabindex="-1"><a class="header-anchor" href="#额外验证-3"><span>额外验证</span></a></h4>
<p>最后，如果你想执行额外的验证逻辑，可以给 <code v-pre>validate</code> 参数传递一个闭包：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$name</span> <span class="token operator">=</span> <span class="token function">suggest</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token argument-name">label</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'What is your name?'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">options</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'Taylor'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Dayle'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">validate</span><span class="token punctuation">:</span> <span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token keyword type-hint">string</span> <span class="token variable">$value</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token keyword">match</span> <span class="token punctuation">(</span><span class="token constant boolean">true</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token function">strlen</span><span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">)</span> <span class="token operator">&lt;</span> <span class="token number">3</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'The name must be at least 3 characters.'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token function">strlen</span><span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">)</span> <span class="token operator">></span> <span class="token number">255</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'The name must not exceed 255 characters.'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token keyword">default</span> <span class="token operator">=></span> <span class="token constant">null</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>闭包接收输入的值，可能会返回一个错误信息，如果验证通过会返回 <code v-pre>null</code>。</p>
<p>或者，你也可以使用 Laravel <a href="https://learnku.com/docs/laravel/11.x/validation" target="_blank" rel="noopener noreferrer">验证器</a> 这个强大功能。要想使用它，你可以给 <code v-pre>validate</code> 传递一个数组，数组包含属性名称和所需的验证规则：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$name</span> <span class="token operator">=</span> <span class="token function">suggest</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token argument-name">label</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'What is your name?'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">options</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'Taylor'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Dayle'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">validate</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required|min:3|max:255'</span><span class="token punctuation">]</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="搜索" tabindex="-1"><a class="header-anchor" href="#搜索"><span>搜索</span></a></h3>
<p>如果你有大量选项供用户选择，在用户使用箭头键选中一个选项之前，可以使用 <code v-pre>search</code> 函数让用户输入一个搜索查询条件来过滤结果：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token keyword">function</span> <span class="token package">Laravel<span class="token punctuation">\</span>Prompts<span class="token punctuation">\</span>search</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$id</span> <span class="token operator">=</span> <span class="token function">search</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token string single-quoted-string">'Search for the user that should receive the mail'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token keyword type-hint">string</span> <span class="token variable">$value</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token function">strlen</span><span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">)</span> <span class="token operator">></span> <span class="token number">0</span></span>
<span class="line">        <span class="token operator">?</span> <span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'like'</span><span class="token punctuation">,</span> <span class="token string double-quoted-string">"%<span class="token interpolation"><span class="token punctuation">{</span><span class="token variable">$value</span><span class="token punctuation">}</span></span>%"</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">pluck</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'id'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>闭包接收用户到目前为止输入的文本，而且必须返回一个选项数组。如果返回一个关联数组，将返回选中选项的键，否则将返回它们的值：</p>
<p>你也可以引入占位文本和信息提示：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$id</span> <span class="token operator">=</span> <span class="token function">search</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token argument-name">label</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'Search for the user that should receive the mail'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">placeholder</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'E.g. Taylor Otwell'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">options</span><span class="token punctuation">:</span> <span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token keyword type-hint">string</span> <span class="token variable">$value</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token function">strlen</span><span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">)</span> <span class="token operator">></span> <span class="token number">0</span></span>
<span class="line">        <span class="token operator">?</span> <span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'like'</span><span class="token punctuation">,</span> <span class="token string double-quoted-string">"%<span class="token interpolation"><span class="token punctuation">{</span><span class="token variable">$value</span><span class="token punctuation">}</span></span>%"</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">pluck</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'id'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">hint</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'The user will receive an email immediately.'</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>当列表开始滚动前，最多展示五个选项。可以通过传递 <code v-pre>scroll</code> 参数自定义这个值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$id</span> <span class="token operator">=</span> <span class="token function">search</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token argument-name">label</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'Search for the user that should receive the mail'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">options</span><span class="token punctuation">:</span> <span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token keyword type-hint">string</span> <span class="token variable">$value</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token function">strlen</span><span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">)</span> <span class="token operator">></span> <span class="token number">0</span></span>
<span class="line">        <span class="token operator">?</span> <span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'like'</span><span class="token punctuation">,</span> <span class="token string double-quoted-string">"%<span class="token interpolation"><span class="token punctuation">{</span><span class="token variable">$value</span><span class="token punctuation">}</span></span>%"</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">pluck</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'id'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">scroll</span><span class="token punctuation">:</span> <span class="token number">10</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="验证-2" tabindex="-1"><a class="header-anchor" href="#验证-2"><span>验证</span></a></h4>
<p>如果你想执行额外验证逻辑，可以给 <code v-pre>validate</code> 参数传递一个闭包：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$id</span> <span class="token operator">=</span> <span class="token function">search</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token argument-name">label</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'Search for the user that should receive the mail'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">options</span><span class="token punctuation">:</span> <span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token keyword type-hint">string</span> <span class="token variable">$value</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token function">strlen</span><span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">)</span> <span class="token operator">></span> <span class="token number">0</span></span>
<span class="line">        <span class="token operator">?</span> <span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'like'</span><span class="token punctuation">,</span> <span class="token string double-quoted-string">"%<span class="token interpolation"><span class="token punctuation">{</span><span class="token variable">$value</span><span class="token punctuation">}</span></span>%"</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">pluck</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'id'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">validate</span><span class="token punctuation">:</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token keyword type-declaration">int</span><span class="token operator">|</span><span class="token keyword type-declaration">string</span> <span class="token variable">$value</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$user</span> <span class="token operator">=</span> <span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">findOrFail</span><span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$user</span><span class="token operator">-></span><span class="token property">opted_out</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token keyword">return</span> <span class="token string single-quoted-string">'This user has opted-out of receiving mail.'</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token punctuation">}</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果 <code v-pre>options</code> 参数是一个关联数组，闭包将接收选中的键，否则将接收选中的值。闭包可能会返回一个错误信息，如果验证通过则返回 <code v-pre>null</code>。</p>
<h3 id="多选搜索" tabindex="-1"><a class="header-anchor" href="#多选搜索"><span>多选搜索</span></a></h3>
<p>如果你有大量可查找的选项，并且需要用户能够选择多个选项，在用户使用箭头键和空格键选中选项之前，可以使用 <code v-pre>multisearch</code> 函数让用户输入一个搜索查询条件来过滤结果：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token keyword">function</span> <span class="token package">Laravel<span class="token punctuation">\</span>Prompts<span class="token punctuation">\</span>multisearch</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$ids</span> <span class="token operator">=</span> <span class="token function">multisearch</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token string single-quoted-string">'Search for the users that should receive the mail'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token keyword type-hint">string</span> <span class="token variable">$value</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token function">strlen</span><span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">)</span> <span class="token operator">></span> <span class="token number">0</span></span>
<span class="line">        <span class="token operator">?</span> <span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'like'</span><span class="token punctuation">,</span> <span class="token string double-quoted-string">"%<span class="token interpolation"><span class="token punctuation">{</span><span class="token variable">$value</span><span class="token punctuation">}</span></span>%"</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">pluck</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'id'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>闭包接收用户到目前为止输入的文本，而且必须返回一个选项数组。如果返回一个关联数组，将返回选中选项的键，否则将返回它们的值：</p>
<p>你也可以引入占位文本和信息提示：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$ids</span> <span class="token operator">=</span> <span class="token function">multisearch</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token argument-name">label</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'Search for the users that should receive the mail'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">placeholder</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'E.g. Taylor Otwell'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">options</span><span class="token punctuation">:</span> <span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token keyword type-hint">string</span> <span class="token variable">$value</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token function">strlen</span><span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">)</span> <span class="token operator">></span> <span class="token number">0</span></span>
<span class="line">        <span class="token operator">?</span> <span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'like'</span><span class="token punctuation">,</span> <span class="token string double-quoted-string">"%<span class="token interpolation"><span class="token punctuation">{</span><span class="token variable">$value</span><span class="token punctuation">}</span></span>%"</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">pluck</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'id'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">hint</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'The user will receive an email immediately.'</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>当列表开始滚动前，最多展示五个选项。可以通过传递 <code v-pre>scroll</code> 参数自定义这个值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$ids</span> <span class="token operator">=</span> <span class="token function">multisearch</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token argument-name">label</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'Search for the users that should receive the mail'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">options</span><span class="token punctuation">:</span> <span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token keyword type-hint">string</span> <span class="token variable">$value</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token function">strlen</span><span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">)</span> <span class="token operator">></span> <span class="token number">0</span></span>
<span class="line">        <span class="token operator">?</span> <span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'like'</span><span class="token punctuation">,</span> <span class="token string double-quoted-string">"%<span class="token interpolation"><span class="token punctuation">{</span><span class="token variable">$value</span><span class="token punctuation">}</span></span>%"</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">pluck</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'id'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">scroll</span><span class="token punctuation">:</span> <span class="token number">10</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="强制选择至少一个值-1" tabindex="-1"><a class="header-anchor" href="#强制选择至少一个值-1"><span>强制选择至少一个值</span></a></h4>
<p>默认情况下，用户可以选择零个或多个选项。你可以通过传递 <code v-pre>required</code> 参数，强制用户选择一个或多个选项：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$ids</span> <span class="token operator">=</span> <span class="token function">multisearch</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token string single-quoted-string">'Search for the users that should receive the mail'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token keyword type-hint">string</span> <span class="token variable">$value</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token function">strlen</span><span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">)</span> <span class="token operator">></span> <span class="token number">0</span></span>
<span class="line">        <span class="token operator">?</span> <span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'like'</span><span class="token punctuation">,</span> <span class="token string double-quoted-string">"%<span class="token interpolation"><span class="token punctuation">{</span><span class="token variable">$value</span><span class="token punctuation">}</span></span>%"</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">pluck</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'id'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">required</span><span class="token punctuation">:</span> <span class="token constant boolean">true</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果你想自定义验证信息，可以给 <code v-pre>required</code> 参数传递一个字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$ids</span> <span class="token operator">=</span> <span class="token function">multisearch</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token string single-quoted-string">'Search for the users that should receive the mail'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token keyword type-hint">string</span> <span class="token variable">$value</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token function">strlen</span><span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">)</span> <span class="token operator">></span> <span class="token number">0</span></span>
<span class="line">        <span class="token operator">?</span> <span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'like'</span><span class="token punctuation">,</span> <span class="token string double-quoted-string">"%<span class="token interpolation"><span class="token punctuation">{</span><span class="token variable">$value</span><span class="token punctuation">}</span></span>%"</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">pluck</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'id'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">required</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'You must select at least one user.'</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="验证-3" tabindex="-1"><a class="header-anchor" href="#验证-3"><span>验证</span></a></h4>
<p>如果你想执行额外的验证逻辑，可以给 <code v-pre>validate</code> 参数传递一个闭包：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$ids</span> <span class="token operator">=</span> <span class="token function">multisearch</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token argument-name">label</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'Search for the users that should receive the mail'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">options</span><span class="token punctuation">:</span> <span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token keyword type-hint">string</span> <span class="token variable">$value</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token function">strlen</span><span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">)</span> <span class="token operator">></span> <span class="token number">0</span></span>
<span class="line">        <span class="token operator">?</span> <span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'like'</span><span class="token punctuation">,</span> <span class="token string double-quoted-string">"%<span class="token interpolation"><span class="token punctuation">{</span><span class="token variable">$value</span><span class="token punctuation">}</span></span>%"</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">pluck</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'id'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">validate</span><span class="token punctuation">:</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token keyword type-hint">array</span> <span class="token variable">$values</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$optedOut</span> <span class="token operator">=</span> <span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'like'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'%a%'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">findMany</span><span class="token punctuation">(</span><span class="token variable">$values</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$optedOut</span><span class="token operator">-></span><span class="token function">isNotEmpty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token keyword">return</span> <span class="token variable">$optedOut</span><span class="token operator">-></span><span class="token function">pluck</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">join</span><span class="token punctuation">(</span><span class="token string single-quoted-string">', '</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">', and '</span><span class="token punctuation">)</span><span class="token operator">.</span><span class="token string single-quoted-string">' have opted out.'</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token punctuation">}</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果 <code v-pre>options</code> 闭包返回一个关联数组，闭包将接收选中的键，否则将接收选中的值。闭包可能会返回一个错误信息，如果验证通过则返回 <code v-pre>null</code>。</p>
<h3 id="暂停" tabindex="-1"><a class="header-anchor" href="#暂停"><span>暂停</span></a></h3>
<p><code v-pre>pause</code> 函数可用户向用户展示信息文本，通过按下 回车 / 返回 键，等待用户确认其继续操作的意愿：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token keyword">function</span> <span class="token package">Laravel<span class="token punctuation">\</span>Prompts<span class="token punctuation">\</span>pause</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token function">pause</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Press ENTER to continue.'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="表单" tabindex="-1"><a class="header-anchor" href="#表单"><span>表单</span></a></h2>
<p>通常，在执行其他操作之前，将按顺序显示多个提示，以收集信息。你可以使用 <code v-pre>form</code> 函数创建一组提示，供用户完成：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token keyword">function</span> <span class="token package">Laravel<span class="token punctuation">\</span>Prompts<span class="token punctuation">\</span>form</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$responses</span> <span class="token operator">=</span> <span class="token function">form</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">text</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'What is your name?'</span><span class="token punctuation">,</span> <span class="token argument-name">required</span><span class="token punctuation">:</span> <span class="token constant boolean">true</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">password</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'What is your password?'</span><span class="token punctuation">,</span> <span class="token argument-name">validate</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'password'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'min:8'</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">confirm</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Do you accept the terms?'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">submit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>submit</code> 方法将返回一个数字索引数组，其中包含来自表单提示的所有响应。不过，你可以通过 <code v-pre>name</code> 参数为每个提示提供一个名称。当提供了一个名称时，可以通过该名称访问指定提示的响应:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>User</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token keyword">function</span> <span class="token package">Laravel<span class="token punctuation">\</span>Prompts<span class="token punctuation">\</span>form</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$responses</span> <span class="token operator">=</span> <span class="token function">form</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">text</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'What is your name?'</span><span class="token punctuation">,</span> <span class="token argument-name">required</span><span class="token punctuation">:</span> <span class="token constant boolean">true</span><span class="token punctuation">,</span> <span class="token argument-name">name</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">password</span><span class="token punctuation">(</span></span>
<span class="line">        <span class="token string single-quoted-string">'What is your password?'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token argument-name">validate</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'password'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'min:8'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token argument-name">name</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'password'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">confirm</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Do you accept the terms?'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">submit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">create</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token variable">$responses</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'password'</span> <span class="token operator">=></span> <span class="token variable">$responses</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'password'</span><span class="token punctuation">]</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>使用 <code v-pre>form</code> 函数最大的好处在于，它能够让用户在表单中通过使用 <code v-pre>CTRL + U</code> 键，返回到之前的提示。这让用户可以调整错误，或者修改选项，而不用取消或者重新启动整个表单。</p>
<p>如果需要对表单中的提示进行更细粒度的控制，可以调用 <code v-pre>add</code> 方法，而不是直接调用某个提示函数。用户以前提供的所有响应都会传递给 <code v-pre>add</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token keyword">function</span> <span class="token package">Laravel<span class="token punctuation">\</span>Prompts<span class="token punctuation">\</span>form</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token keyword">function</span> <span class="token package">Laravel<span class="token punctuation">\</span>Prompts<span class="token punctuation">\</span>outro</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$responses</span> <span class="token operator">=</span> <span class="token function">form</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">text</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'What is your name?'</span><span class="token punctuation">,</span> <span class="token argument-name">required</span><span class="token punctuation">:</span> <span class="token constant boolean">true</span><span class="token punctuation">,</span> <span class="token argument-name">name</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$responses</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token function">text</span><span class="token punctuation">(</span><span class="token string double-quoted-string">"How old are you, <span class="token interpolation"><span class="token punctuation">{</span><span class="token variable">$responses</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">]</span><span class="token punctuation">}</span></span>?"</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token argument-name">name</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'age'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">submit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token function">outro</span><span class="token punctuation">(</span><span class="token string double-quoted-string">"Your name is <span class="token interpolation"><span class="token punctuation">{</span><span class="token variable">$responses</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">]</span><span class="token punctuation">}</span></span> and you are <span class="token interpolation"><span class="token punctuation">{</span><span class="token variable">$responses</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'age'</span><span class="token punctuation">]</span><span class="token punctuation">}</span></span> years old."</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="提示信息" tabindex="-1"><a class="header-anchor" href="#提示信息"><span>提示信息</span></a></h2>
<p><code v-pre>note</code>、<code v-pre>info</code>、<code v-pre>warning</code>、<code v-pre>error</code> 和 <code v-pre>alert</code> 函数可以用来展示提示信息：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token keyword">function</span> <span class="token package">Laravel<span class="token punctuation">\</span>Prompts<span class="token punctuation">\</span>info</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token function">info</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Package installed successfully.'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="表格" tabindex="-1"><a class="header-anchor" href="#表格"><span>表格</span></a></h2>
<p><code v-pre>table</code> 函数让展示多行多列的数据变得十分简单。你唯一要做的就是为表格提供列名称和数据：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token keyword">function</span> <span class="token package">Laravel<span class="token punctuation">\</span>Prompts<span class="token punctuation">\</span>table</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token function">table</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token punctuation">[</span><span class="token string single-quoted-string">'Name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Email'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'email'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">toArray</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="旋转" tabindex="-1"><a class="header-anchor" href="#旋转"><span>旋转</span></a></h2>
<p>在执行指定的回调时，<code v-pre>spin</code> 函数会显示一个带有可选消息的旋转器。它用于指示正在运行的进程，并在完成时返回回调执行的结果:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token keyword">function</span> <span class="token package">Laravel<span class="token punctuation">\</span>Prompts<span class="token punctuation">\</span>spin</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$response</span> <span class="token operator">=</span> <span class="token function">spin</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'http://example.com'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'Fetching response...'</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p><strong>警告</strong><br>
<code v-pre>spin</code> 函数要求开启 <code v-pre>pcntl</code> PHP 扩展，才能让旋转器动起来。当扩展不可用时，将会用一个静态版本的旋转器代替。</p>
</blockquote>
<h2 id="进度条" tabindex="-1"><a class="header-anchor" href="#进度条"><span>进度条</span></a></h2>
<p>对于长时间运行的任务，给用户展示一个进度条是很有帮助的，这可以告知用户任务完成的情况。通过使用 <code v-pre>progress</code> 函数，Laravel 会展示一个进度条，并在给定的迭代值上推进每次迭代的进度：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token keyword">function</span> <span class="token package">Laravel<span class="token punctuation">\</span>Prompts<span class="token punctuation">\</span>progress</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$users</span> <span class="token operator">=</span> <span class="token function">progress</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token argument-name">label</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'Updating users'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">steps</span><span class="token punctuation">:</span> <span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">callback</span><span class="token punctuation">:</span> <span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token variable">$user</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">performTask</span><span class="token punctuation">(</span><span class="token variable">$user</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>progress</code> 函数的作用类似于一个映射函数，它会返回一个数组，包含你的回调每次迭代的返回值：</p>
<p>回调也可以接受 <code v-pre>\Laravel\Prompts\Progress</code> 的实例，这允许你修改标签并在每次迭代中给出提示：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$users</span> <span class="token operator">=</span> <span class="token function">progress</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token argument-name">label</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'Updating users'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">steps</span><span class="token punctuation">:</span> <span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">callback</span><span class="token punctuation">:</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$user</span><span class="token punctuation">,</span> <span class="token variable">$progress</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$progress</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">label</span><span class="token punctuation">(</span><span class="token string double-quoted-string">"Updating <span class="token interpolation"><span class="token punctuation">{</span><span class="token variable">$user</span><span class="token operator">-></span><span class="token property">name</span><span class="token punctuation">}</span></span>"</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">hint</span><span class="token punctuation">(</span><span class="token string double-quoted-string">"Created on <span class="token interpolation"><span class="token punctuation">{</span><span class="token variable">$user</span><span class="token operator">-></span><span class="token property">created_at</span><span class="token punctuation">}</span></span>"</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">performTask</span><span class="token punctuation">(</span><span class="token variable">$user</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">hint</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'This may take some time.'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>有时，针对进度条如何推进，你可能需要更多的手动控制。为此，首先，定义流程需要迭代的步骤总数。然后，在处理完每个任务后，通过 <code v-pre>advance</code> 函数推进进度条：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$progress</span> <span class="token operator">=</span> <span class="token function">progress</span><span class="token punctuation">(</span><span class="token argument-name">label</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'Updating users'</span><span class="token punctuation">,</span> <span class="token argument-name">steps</span><span class="token punctuation">:</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$users</span> <span class="token operator">=</span> <span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$progress</span><span class="token operator">-></span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">foreach</span> <span class="token punctuation">(</span><span class="token variable">$users</span> <span class="token keyword">as</span> <span class="token variable">$user</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">performTask</span><span class="token punctuation">(</span><span class="token variable">$user</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$progress</span><span class="token operator">-></span><span class="token function">advance</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$progress</span><span class="token operator">-></span><span class="token function">finish</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="终端考虑因素" tabindex="-1"><a class="header-anchor" href="#终端考虑因素"><span>终端考虑因素</span></a></h2>
<h4 id="终端宽度" tabindex="-1"><a class="header-anchor" href="#终端宽度"><span>终端宽度</span></a></h4>
<p>如果任何标签、选项或验证消息的长度超过用户终端中「列」的数量，为了适应都将被自动截断。如果你的用户可能使用较窄的终端，请考虑最小化这些字符串的长度。对于支持 80 个字符长度的终端来说，通常安全的最大长度是 74 个字符。</p>
<h4 id="终端高度" tabindex="-1"><a class="header-anchor" href="#终端高度"><span>终端高度</span></a></h4>
<p>为了适应用户终端的高度，任何接受 <code v-pre>scroll</code> 参数的提示，配置的值都会被自动减小，包括验证信息的空格。</p>
<h2 id="不支持的环境与回退" tabindex="-1"><a class="header-anchor" href="#不支持的环境与回退"><span>不支持的环境与回退</span></a></h2>
<p>Laravel Prompts 通过 WSL 支持 macOS、 Linux 和 Windows。由于 PHP Windows 版本的限制，目前不可能在 WSL 之外的 Windows 上使用 Laravel Prompts 。</p>
<p>出于这个原因，Laravel Prompts 支持回退到另一种实现，例如 <a href="https://symfony.com/doc/7.0/components/console/helpers/questionhelper.html" target="_blank" rel="noopener noreferrer">Symfony Console Question Helper</a> 。</p>
<blockquote>
<p><strong>注意</strong><br>
当在 Laravel 框架中使用 Laravel Prompts 时，每个提示的回退都已经为你配置好，当环境不支持时会自动启用。</p>
</blockquote>
<h4 id="回退条件" tabindex="-1"><a class="header-anchor" href="#回退条件"><span>回退条件</span></a></h4>
<p>如果你没有使用 Laravel 或者需要自定义何时使用回退行为，可以给 <code v-pre>Prompt</code> 类的 <code v-pre>fallbackWhen</code> 静态方法传递一个布尔值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Prompts<span class="token punctuation">\</span>Prompt</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Prompt</span><span class="token operator">::</span><span class="token function">fallbackWhen</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token operator">!</span> <span class="token variable">$input</span><span class="token operator">-></span><span class="token function">isInteractive</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">||</span> <span class="token class-name">windows_os</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">||</span> <span class="token class-name">app</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">runningUnitTests</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="回退行为" tabindex="-1"><a class="header-anchor" href="#回退行为"><span>回退行为</span></a></h4>
<p>如果你没有使用 Laravel 或者需要自定义回退行为，可以给每个提示类的 <code v-pre>fallbackUsing</code> 静态方法传递一个闭包：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Prompts<span class="token punctuation">\</span>TextPrompt</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Symfony<span class="token punctuation">\</span>Component<span class="token punctuation">\</span>Console<span class="token punctuation">\</span>Question<span class="token punctuation">\</span>Question</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Symfony<span class="token punctuation">\</span>Component<span class="token punctuation">\</span>Console<span class="token punctuation">\</span>Style<span class="token punctuation">\</span>SymfonyStyle</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">TextPrompt</span><span class="token operator">::</span><span class="token function">fallbackUsing</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">TextPrompt</span> <span class="token variable">$prompt</span><span class="token punctuation">)</span> <span class="token keyword">use</span> <span class="token punctuation">(</span><span class="token variable">$input</span><span class="token punctuation">,</span> <span class="token variable">$output</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$question</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Question</span><span class="token punctuation">(</span><span class="token variable">$prompt</span><span class="token operator">-></span><span class="token property">label</span><span class="token punctuation">,</span> <span class="token variable">$prompt</span><span class="token operator">-></span><span class="token property">default</span> <span class="token operator">?</span><span class="token punctuation">:</span> <span class="token constant">null</span><span class="token punctuation">)</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token operator">-></span><span class="token function">setValidator</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$answer</span><span class="token punctuation">)</span> <span class="token keyword">use</span> <span class="token punctuation">(</span><span class="token variable">$prompt</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$prompt</span><span class="token operator">-></span><span class="token property">required</span> <span class="token operator">&amp;&amp;</span> <span class="token variable">$answer</span> <span class="token operator">===</span> <span class="token constant">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">                <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name class-name-fully-qualified"><span class="token punctuation">\</span>RuntimeException</span><span class="token punctuation">(</span><span class="token function">is_string</span><span class="token punctuation">(</span><span class="token variable">$prompt</span><span class="token operator">-></span><span class="token property">required</span><span class="token punctuation">)</span> <span class="token operator">?</span> <span class="token variable">$prompt</span><span class="token operator">-></span><span class="token property">required</span> <span class="token punctuation">:</span> <span class="token string single-quoted-string">'Required.'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">            <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">            <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$prompt</span><span class="token operator">-></span><span class="token property">validate</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">                <span class="token variable">$error</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token variable">$prompt</span><span class="token operator">-></span><span class="token property">validate</span><span class="token punctuation">)</span><span class="token punctuation">(</span><span class="token variable">$answer</span> <span class="token operator">??</span> <span class="token string single-quoted-string">''</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">                <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$error</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">                    <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name class-name-fully-qualified"><span class="token punctuation">\</span>RuntimeException</span><span class="token punctuation">(</span><span class="token variable">$error</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">                <span class="token punctuation">}</span></span>
<span class="line">            <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">            <span class="token keyword">return</span> <span class="token variable">$answer</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">SymfonyStyle</span><span class="token punctuation">(</span><span class="token variable">$input</span><span class="token punctuation">,</span> <span class="token variable">$output</span><span class="token punctuation">)</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token operator">-></span><span class="token function">askQuestion</span><span class="token punctuation">(</span><span class="token variable">$question</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>回退闭包必须针对每个提示类单独配置。闭包将接收提示类的实例，并且必须为提示返回适当的类型。</p>
<blockquote>
<p>本译文仅用于学习和交流目的，转载请务必注明文章译者、出处、和本文链接<br>
我们的翻译工作遵照 <a href="https://learnku.com/docs/guide/cc4.0/6589" target="_blank" rel="noopener noreferrer">CC 协议</a>，如果我们的工作有侵犯到您的权益，请及时联系我们。</p>
</blockquote>
<hr>
<blockquote>
<p>原文地址：<a href="https://learnku.com/docs/laravel/11.x/promptsmd/16728" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/11.x/pr...</a></p>
<p>译文地址：<a href="https://learnku.com/docs/laravel/11.x/promptsmd/16728" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/11.x/pr...</a></p>
</blockquote>
</div></template>


